<?php
/**
 * Created by PhpStorm.
 * User: Shen
 * Date: 2016/7/18
 * Time: 21:29
 * PSR-0的基础框架
 */
define('BASEDIR',__DIR__);
include BASEDIR.'/DuiCode/Loader.php';
//自动载入技术
spl_autoload_register('\\DuiCode\\Loader::autoload');


DuiCode\Object::test();

App\Controller\Home\Index::test();


/**策略模式**/
class Page{
    protected $strategy;
    function index(){
        echo "AD:";
        $this->strategy->showAd();
        echo "<br />";

        echo "Category:";
        $this->strategy->showCategory();
        echo "<br />";
    }

    function setStrategy(\DuiCode\UserStrategy $strategy){
        $this->strategy = $strategy;
    }
}

$page = new Page;
if (isset($_GET['female'])){
    $strategy = new \DuiCode\FemaleUserStrategy();
}else{
    $strategy = new \DuiCode\MaleUserStrategy();
}
$page->setStrategy($strategy);



/**
 *
 * 观察者模式
 *
 */
class Event extends \DuiCode\EventGenerator{
    function trigger(){
        echo "Event<br/>\n";

        /**传统模式update
        echo "逻辑1<br/>\n";

        echo "逻辑2<br/>\n";

        echo "逻辑3<br/>\n";
         **/

        //发送通知
        $this->notify();

    }
}


class Observer1 implements \DuiCode\Observer{
    function update($event_info = null){
        // TODO: Implement update() method.
        echo "逻辑1<br/>\n";

    }
}

$event = new Event();
$event->addObserver(new Observer1());
$event->trigger();


/**
 *原型模式：与工厂模式类似，都是用来创建对象，与工厂模式的实现不同，原型模式是先创建好一个原型对象，
 * 然后通过clone原型对象来创建新的对象。这样就免去了类创建时重复的初始化操作。
 * 原型模式适用于大对象的创建，创建一个大对象需要很大的开销，如果每次new就会消耗很大，
 * 原型模式仅需内存拷贝即可。
 */

$canvas1 = new \DuiCode\Canvas();
$canvas1->init();
$canvas1->addDecorator(new \DuiCode\ColorDrawDecorator());
$canvas1->addDecorator(new \DuiCode\SizeDrawDecorator());
$canvas1->rect(3, 6, 4, 12);
$canvas1->draw();

echo '<hr /><br />\n';

//传统方式
$canvas2 = new \DuiCode\Canvas();
$canvas2->init();
$canvas2->rect(1, 3, 2, 6);
$canvas2->draw();

echo '<hr /><br />\n';

//原型模式
$prototype = new \DuiCode\Canvas();
$prototype->init();

$canvas3 = clone $prototype;
$canvas3->init();
$canvas3->rect(3, 6, 4, 12);
$canvas3->draw();

echo '<hr /><br />\n';

$canvas4 = clone $prototype;
$canvas4->init();
$canvas4->rect(1, 3, 2, 6);
$canvas4->draw();








